#include <iostream>
#include <fstream>
#include <iomanip>
#include "Logger.h"

extern "C"
{
#include <time.h>
}

namespace RawProject
{

	Logger::Logger(const char * fileName) : _logFile(fileName,std::ios::app | std::ios::out)
	{
		_logFile << std::setfill('0');
	}

	Logger::~Logger()
	{
		_logFile.close();
	}

	void Logger::Log(std::istream & txt, LogLevel lvl)
	{
		std::string cat;
		char a = 0;

		do
		{
			txt.read(&a,1);
			cat += a;
		}
		while (a != 10);

		cat = cat.substr(0,cat.find(0x0a/*Remove line feed*/));

		this->Log(cat);
	}

	void Logger::Log(std::string txt, LogLevel lvl)
	{
		time_t rawtime;
		struct tm * timeinfo;

		time ( &rawtime );
		timeinfo = localtime ( &rawtime );

		_logFile << std::setw(4) << timeinfo->tm_year + 1900 << "/" << std::setw(2) <<  timeinfo->tm_mon + 1 << "/" << std::setw(2) << timeinfo->tm_mday << " " ;
		_logFile << std::setw(2) << timeinfo->tm_hour << ":" << std::setw(2) << timeinfo->tm_min << ":" << std::setw(2) << timeinfo->tm_sec << " : ";

		switch(lvl)
		{
		case Verbose:
			_logFile << "VERBOSE : ";
			break;
		case Warning:
			_logFile << "WARNING : ";
			break;
		case Error:
			_logFile << "ERROR   : ";
			break;
		}

		_logFile << txt.c_str() << std::endl;

#if _DEBUG
		std::cout << txt.c_str() << std::endl;
#endif

	}

	Logger globalLogger("RawProject.log");
}
